package com.lookme.lmtool.cracker.filter.noise;

import com.lookme.lmtool.cracker.CrackerConstants;
import com.lookme.lmtool.cracker.annotation.FilterAnnotation;
import com.lookme.lmtool.cracker.annotation.ParamAnnotation;
import com.lookme.lmtool.cracker.filter.AbstractFilter;
import com.lookme.lmtool.utils.ImageUtils;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;

@FilterAnnotation(groupName = CrackerConstants.FILTER_GROUP_NAME_NOISE,name="去除干扰线")
public class FilterCleaLineNoise extends AbstractFilter {

    @XStreamAsAttribute
    @ParamAnnotation(name = "黑点检测数量", desc = "点周围黑点的数量",inputType = ParamAnnotation.InputType.Spinner)
    int threshold=2;
    @XStreamAsAttribute
    @ParamAnnotation(name = "检测斜角", desc = "检测点的四个斜角",inputType = ParamAnnotation.InputType.Radio,width = 45)
    boolean checkBevel=false;

    @Override
    public BufferedImageOp getFilter() {
        return null;
    }

    @Override
    public BufferedImage filter(BufferedImage src, BufferedImage dest) {
        int h=src.getHeight();
        int w=src.getWidth();
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                int pointflagNum = 0;//去除点判定
                if (ImageUtils.isBlack(src.getRGB(x, y))) {
                    //检测左边像素点
                    if (x!=0&&ImageUtils.isBlack(src.getRGB(x - 1, y))) {
                        pointflagNum++;
                    }
                    //检测右边像素点
                    if (x!=w-1&&ImageUtils.isBlack(src.getRGB(x + 1, y))) {
                        pointflagNum++;
                    }
                    //检测上边像素点
                    if (y!=0&&ImageUtils.isBlack(src.getRGB(x, y - 1))) {
                        pointflagNum++;
                    }
                    //检测下边像素点
                    if (y!=h-1&&ImageUtils.isBlack(src.getRGB(x, y + 1))) {
                        pointflagNum++;
                    }
                    if (checkBevel) {
                        //检测左上像素点
                        if (x!=0&&y!=0&&ImageUtils.isBlack(src.getRGB(x - 1, y - 1))) {
                            pointflagNum++;
                        }
                        //检测左下像素点
                        if (x!=0&&y!=h-1&&ImageUtils.isBlack(src.getRGB(x - 1, y + 1))) {
                            pointflagNum++;
                        }
                        //检测右上像素点
                        if (x!=w-1&&y!=0&&ImageUtils.isBlack(src.getRGB(x + 1, y - 1))) {
                            pointflagNum++;
                        }
                        //检测右下像素点
                        if (x!=w-1&&y!=h-1&&ImageUtils.isBlack(src.getRGB(x + 1, y + 1))) {
                            pointflagNum++;
                        }
                    }
                    //去除干扰点
                    if (pointflagNum < threshold) {
                        dest.setRGB(x, y, -1);
                    }
                }
            }
        }
        return dest;
    }
}
